package com.g17.virtualpetbackend.controller;

import com.g17.virtualpetbackend.entity.Record;
import com.g17.virtualpetbackend.service.RecordService;
import com.g17.virtualpetbackend.utilty.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class RecordController {

    private static final Logger logger = LoggerFactory.getLogger(RecordController.class);

    @Autowired
    RecordService recordService;

    @GetMapping("/record")
    public List<Record> getRecord(@RequestParam(value = "id",required = false) Long id, @RequestParam(value = "pet_name",required = false) String pet_name, @RequestParam(value = "keyword", required = false) String keyword){
        ArrayList<Record> records = new ArrayList<>();
        if (id != null){
            logger.info(id.toString());
            Optional<Record> recordById = recordService.findRecordById(id);
            recordById.ifPresent(records::add);
        }
        else if (pet_name!=null){
            logger.info(pet_name);
            records.add(recordService.findRecordByPetName(pet_name));
        }
        else if(keyword!=null){
            logger.info(keyword);
            records.addAll(recordService.findRecordByKeyword(keyword));
        }
        return records;
    }

    @GetMapping("/allRecord")
    public List<Record> getAllRecords(){
        return recordService.findAllRecords();
    }

    @GetMapping("/singleRecord")
    public Record getSingleRecord(@RequestParam(value = "id") Long id){
        return recordService.findSingleRecord(id);
    }



    @PostMapping("/record")
    public Response changeRecord(@RequestParam(name = "id", required = false) Long id,
                               @RequestParam(name = "pet_name", required = false) String pet_name,
                               @RequestParam(name = "master_name", required = false) String master_name,
                               @RequestParam(name = "disease_name", required = false) String disease_name,
                               @RequestParam(name = "species", required = false) String species,
                               @RequestParam(name = "symptom", required = false) String symptom,
                               @RequestParam(name = "symptom_picture", required = false) List<String> symptom_picture,
                               @RequestParam(name = "symptom_video", required = false) String symptom_video,
                               @RequestParam(name = "diagnose", required = false) String diagnose,
                               @RequestParam(name = "treatment", required = false) String treatment,
                               @RequestParam(name = "medicine", required = false) String medicine,
                               @RequestParam(name = "followup", required = false) String followup,
                               @RequestParam(name = "followup_picture", required = false) List<String> followup_picture,
                               @RequestParam(name = "followup_video", required = false) String followup_video,
                               @RequestParam(name = "doctor", required = false) String doctor){
        Record record;
        if (id==null){
            //create

            record = recordService.createRecord(pet_name,master_name,disease_name,species,symptom,symptom_picture,symptom_video,diagnose,treatment,medicine,followup,followup_picture,followup_video,doctor);
        }
        else{
            //update
            record = recordService.updateRecord(id,pet_name,master_name,disease_name,species,symptom,symptom_picture,symptom_video,diagnose,treatment,medicine,followup,followup_picture,followup_video,doctor);
        }

        if (record!=null){
            return new Response(record.getId(),Response.OK);
        }else{
            return new Response(Response.ERROR_ID,Response.ERROR);
        }
    }

    @DeleteMapping("/record")
    public Response deleteRecord(@RequestParam(name = "id") Long id){
        Record record = recordService.deleteRecordById(id);
        if (record!=null){
            return new Response(id,Response.OK);
        }else{
            return new Response(Response.ERROR_ID,Response.ERROR);
        }
    }
}
